package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.MouseUserDevicesinfoBean;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.logic.contract.ChangeMouseSetCotract;
import com.hzdesk.media.koreabatchmatvector.logic.model.ChangeMouseSetModle;
import com.hzdesk.media.koreabatchmatvector.utils.SharedPreferencesUtils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/17.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class ChangeMouseSetPersenter implements ChangeMouseSetCotract.IChangeMouseSetPersenter {
    private ChangeMouseSetCotract.IChangeMouseSetView mIChangeMouseSetView;
    private ChangeMouseSetCotract.IChangeMouseSetModle mIChangeMouseSetModle;
    private List<String> datalist_1 = new LinkedList<>(Arrays.asList("红", "橙", "黄", "绿", "天蓝", "蓝", "紫", "黑"));                                 //颜色下拉框数据
    private List<String> datalist_3 = new LinkedList<>(Arrays.asList("red_line", "orange_line", "yellow_line", "green_line", "cyan_line", "blue_line", "puple_line", "black_line"));  //注意颜色字母第一个大写;
    private MouseUserDevicesinfoBean touseUserDevicesinfoBean ;
    private List<String> datalist_2 = new LinkedList<>(Arrays.asList("1", "3", "5", "7", "9"));
    public ChangeMouseSetPersenter(ChangeMouseSetCotract.IChangeMouseSetView mIChangeMouseSetView) {
        this.mIChangeMouseSetView = mIChangeMouseSetView;
        mIChangeMouseSetModle = new ChangeMouseSetModle();
    }
    private int REQUEST_GET_PATH = 0;
    private int REQUEST_GET_Color = 1;
    private int REQUEST_GET_PAINT_COLOR=2;
    private String paintColor="";


    @Override
    public void onclickedBack() {
        mIChangeMouseSetView.finshPager();
    }

    @Override
    public void onClickedCursorColor() {
        mIChangeMouseSetView.toCursorColorSelectorPager();
        Log.e("ChangeMouseSetPersenter", "onClickedCursorColor ********** : ");
    }

    @Override
    public void onClickedPathSize() {
        mIChangeMouseSetView.toPathSizeSelectorPager();
    }

    @Override
    public void onClickedDisable(Context mContext) {
      String disable =   mIChangeMouseSetView.getDisable();
        int DisMouse = 0;
      if(disable.equals("是")){
          DisMouse = 0;
          mIChangeMouseSetView.setDisable("否");
          mIChangeMouseSetView.showToast("取消禁用");
      }else{
          DisMouse = 1;
          mIChangeMouseSetView.setDisable("是");
          mIChangeMouseSetView.showToast("已禁用");
      }
        if(touseUserDevicesinfoBean != null){
            touseUserDevicesinfoBean.setDisMouse(DisMouse);
            // 更新数据库
            mIChangeMouseSetModle.update(touseUserDevicesinfoBean,mContext);
        }
    }

    @Override
    public void onClickedSaveSetting(Context mContext) {
        SharedPreferencesUtils sharedPreferencesUtils=new SharedPreferencesUtils(mContext);
        int PathSizeIndex = sharedPreferencesUtils.getPathSize(mContext);
       // Log.e("ChangeMouseSetPersenter", "onClickedSaveSetting: "+PathSizeIndex );
        try {
            String name = mIChangeMouseSetView.getInputName();
           String disable =  mIChangeMouseSetView.getDisable();
            int DisMouse = 0;
            if(disable.equals("是")){
                DisMouse = 1;
           }
            String CursorColor  = mIChangeMouseSetView.getCursorColor();
            int Colorindex = datalist_1.indexOf(CursorColor);
            String Color =  datalist_3.get(Colorindex) ;
            int BrushSize = Integer.parseInt(datalist_2.get(PathSizeIndex));
            //将对应鼠标用户的信息更新修改数据库
            if(touseUserDevicesinfoBean != null){
                touseUserDevicesinfoBean.setName(name);
                touseUserDevicesinfoBean.setDisMouse(DisMouse);
                touseUserDevicesinfoBean.setCursorColor(Color);
                touseUserDevicesinfoBean.setBrushSize(BrushSize);
                touseUserDevicesinfoBean.setColor(paintColor);
                // 更新数据库
                mIChangeMouseSetModle.update(touseUserDevicesinfoBean,mContext);
            }
        } catch (NumberFormatException e) {}
        mIChangeMouseSetView.finshPager();
    }

    @Override
    public void initListenting(RelativeLayout mrlItem) {
        mrlItem.setOnHoverListener(onHoverListener);
        mrlItem.setOnFocusChangeListener(onFocusChangeListener);
    }

    @Override
    public void initSettingDate(int position, Context mContext) {
        // 从数据库获取 对应的鼠标数据展示
        List<MouseUserDevicesinfoBean> datelist =  mIChangeMouseSetModle.query(mContext);
        // 遍历集合
        if(datelist != null && datelist.size() >0){
            // 查询对应的鼠标展示鼠标各种信息
            for (MouseUserDevicesinfoBean mouseUserDevicesinfoBean : datelist) {
                if(mouseUserDevicesinfoBean.getMouseNum() == (position+1 )){
                    touseUserDevicesinfoBean = mouseUserDevicesinfoBean;
                    mIChangeMouseSetView.SetTile("设置"+(position+1)+"号鼠标");
                    boolean flag = mouseUserDevicesinfoBean.getDisMouse() ==0?false:true;
                    if(flag){
                        mIChangeMouseSetView.setDisable("是");
                    }else{
                        mIChangeMouseSetView.setDisable("否");
                    }
                    //todo 储存光标颜色索引 和笔触大小索引
                    SharedPreferencesUtils sharedPreferencesUtils=new SharedPreferencesUtils(mContext);
                    try {
                        mIChangeMouseSetView.setName(mouseUserDevicesinfoBean.getName());
                        int mypostion = datalist_3.indexOf(mouseUserDevicesinfoBean.getCursorColor());
                        sharedPreferencesUtils.putDate(AppConfig.OPTION_CURSOR_COLOR,mypostion);
                        int PathSizeindex = datalist_2.indexOf(mouseUserDevicesinfoBean.getBrushSize()+"");
                        sharedPreferencesUtils.putDate(AppConfig.OPTION_path_size,PathSizeindex);
                        mIChangeMouseSetView.setInputName(datalist_1.get(mypostion));
                        mIChangeMouseSetView.setPathSize(datalist_2.get(PathSizeindex));
                        paintColor=mouseUserDevicesinfoBean.getColor();
                        Log.e("lk------",paintColor);
                        mIChangeMouseSetView.changePaintColor(paintColor);
                    } catch (Exception e) {}
                }
            }
        }else{
            mIChangeMouseSetView.SetTile("设置"+position+"号鼠标");
        }
    }

    @Override
    public void initSaveDate(RelativeLayout mrlItem) {
        mrlItem.setOnHoverListener(onHoverListener);
        mrlItem.setOnFocusChangeListener(onFocusChangeListenerSaveSetting);
    }

    @Override
    public void showCursorAndPathSize(Context mContext) {
        SharedPreferencesUtils sharedPreferencesUtils=new SharedPreferencesUtils(mContext);
        int colorIndex = sharedPreferencesUtils.getCursorColor(mContext);
        int PathSizeIndex = sharedPreferencesUtils.getPathSize(mContext);
        //Log.e("ChangeMouseSetPersenter", "showCursorAndPathSize colorIndex: "+colorIndex );
        //Log.e("ChangeMouseSetPersenter", "showCursorAndPathSize PathSizeIndex: "+PathSizeIndex );
        String color = datalist_1.get(colorIndex);
        String size = datalist_2.get(PathSizeIndex);
        try {

            if(touseUserDevicesinfoBean != null){
                touseUserDevicesinfoBean.setCursorColor(datalist_3.get(colorIndex));
                touseUserDevicesinfoBean.setBrushSize(Integer.parseInt(size));
                // 更新数据库
                mIChangeMouseSetModle.update(touseUserDevicesinfoBean,mContext);
            }
        } catch (NumberFormatException e) {}
        mIChangeMouseSetView.showColorAndSize(color,size);
    }

    @Override
    public void onClickedPaintColor() {
        mIChangeMouseSetView.toColorPickerPager();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data!=null){
            Log.d("lk","requestCode:"+requestCode+" resultCode:"+resultCode+" date:"+data.getExtras().getString("paintColor"));
            paintColor=data.getExtras().getString("paintColor");
            if (requestCode==REQUEST_GET_PAINT_COLOR){
                mIChangeMouseSetView.changePaintColor(paintColor);
            }

        }


    }

    //鼠标移动事件
    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    //v.clearFocus();
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };
    //焦点改变事件
    private View.OnFocusChangeListener onFocusChangeListener=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                v.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.02f);
                ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.02f);
                ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            }else {
                v.setBackgroundResource(R.drawable.view_shape);
                ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.02f,1.0f);
                ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.02f,1.0f);
                ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
                AnimatorSet animatorSet_01=new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };
    private View.OnFocusChangeListener onFocusChangeListenerSaveSetting=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus){
                v.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.02f);
                ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.02f);
                ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            }else {
                v.setBackgroundResource(R.drawable.view_shape_01);
                ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.02f,1.0f);
                ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.02f,1.0f);
                ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
                AnimatorSet animatorSet_01=new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };
}
